package com.atguigu.springmvc.demo01.controller;

import com.atguigu.springmvc.demo01.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;

/**
 * @author yanglin
 * @create 2023-12-14 20:47 星期四
 * description:
 */
@Controller
public class ParamController {

    /**
     * 形参位置的request表示当前请求
     */
    @RequestMapping("/testServletPI")
    public String restServletApi(HttpServletRequest request){

        HttpSession session = request.getSession();
        
        //servletAPI获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username + " " + password);
        return "success";
    }

    /**
     * 
     * @RequestParam：请求参数和形参创建映射关系
     *  @RequestHeader： 获取请求头中的信息，并赋值给指定的参数
     */
    @RequestMapping("/testParam")
    public String testParam(@RequestParam("user_name") String username,
                            @RequestParam("password") String password,
                            @RequestParam("hobby") String[] hobby,
                            @RequestHeader("Host") String host,
                            @CookieValue("JSESSIONID") String JSESSIONID){
        //若请求参数中出现多个同名的请求参数，可以在控制器方法的形参位置设置一个字符串类型或字符串数组接收此请求参数
        //若使用字符串类型的形参，最终结果为请求参数的每一个值直接使用逗号拼接
        System.out.println(username + " " + password + " " + Arrays.toString(hobby));
        System.out.println("host:" + host);
        System.out.println(JSESSIONID);
        return "success";
    }
    
    @RequestMapping("/testPojo")
    public String testPojo(User user){
        System.out.println(user);
        return "success";
    }
}
